package com.controller;

import com.bean.dto.Dto;
import com.bean.vo.ItripTokenVO;
import com.service.IItripUserService;
import com.service.TokenService;
import com.util.DtoUtil;
import com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class TokenController {

    @Autowired
    private TokenService tokenService;

    private int expire = 7200;

    @PostMapping("/api/retoken")
    public Dto replace(HttpServletRequest request){
        System.out.println(">>> 用户 --> 置换token");

        //获取token字符串
        String token = request.getHeader("token");
        System.out.println(token);
        //获取设备信息
        String agent = request.getHeader("user-agent");
        System.out.println("agent:" + agent);

        //置换token
        String newToken = tokenService.replace(token, agent);

        if (newToken == null){
            return DtoUtil.returnError("token已失效!");
        }

        return DtoUtil.returnSuccess("登录成功！",new ItripTokenVO(newToken, expire*1000,new Date().getTime()));
    }
}
